package com.xiangban.service.impl;


import com.alibaba.fastjson.JSON;
import com.xiangban.dao.TUserMapper;
import com.xiangban.dao.TUserMappers;
import com.xiangban.exception.BizException;
import com.xiangban.model.TUser;
import com.xiangban.model.TUserOpenId;
import com.xiangban.model.res.ApiResultCode;
import com.xiangban.service.UserInfoService;
import com.xiangban.util.CommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;

import static com.xiangban.utils.CommonUtils.BizEmpty;


/**
 * @Author ly
 * @date 2021/11/16 12:41
 */
@Slf4j
@Service
public class UserInfoServiceImpl implements UserInfoService {
    @Autowired
    private CommonUtil commonUtils;
    @Autowired
    private TUserMapper tUserMapper;
    @Autowired
    TUserMappers tUserMappers;
    @Autowired
    private HttpServletRequest request;

    /**
     * 用户注册
     *
     * @return sccuess
     */
    @Override
    public String UserRegistration(TUser tUser) {
        if (BizEmpty(tUser.getUid())) {
            return "failed";
        }
        TUser tUsers = new TUser();
        tUsers.setUid(tUser.getUid());
        //根据uid查询数据库 空则注册 非空则修改
        TUser tUser1 = tUserMapper.selectOne(tUsers);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = format.format(new Date());
        if (BizEmpty(tUser1)){
            if (!isNULL(tUser)){
                return "failed";
            }
            //将数据加入到user表
            tUser.setCreateTime(time);
            try {
                tUserMapper.insertSelective(tUser);
                return "ok";
            } catch (Exception e) {
                e.printStackTrace();
                return "failed";
            }

        }
        return "failed";

    }
    /**
     * 获取用户信息
     * */
    @Override
    public Object getUserInfo() {
        String userId = commonUtils.getUserId(request);
        TUser tUser = new TUser();
        tUser.setUid(Integer.parseInt(userId));
       TUser userinfo =  tUserMappers.getUserInfoByUid(tUser);

        return userinfo;
    }

    /**
     * 用户修改update
     * */
    @Override
    public Object UserUpdate(TUserOpenId tUserOpenId) {
        log.info(JSON.toJSONString(tUserOpenId));

            //有uid则是修改
//            Example example = new Example(TUser.class);
//            example.createCriteria().andEqualTo("uid",tUser.getUid());
//            tUser.setUpdateTime(time);
//             tUserMapper.updateByExampleSelective(tUser,example);
        if (BizEmpty(tUserOpenId.getUid())){
            throw  new BizException(ApiResultCode.COMMON_PARAM_NULL);
        }

        if (BizEmpty(tUserOpenId.getOpenId())){
            throw  new BizException(ApiResultCode.COMMON_PARAM_NULL);
        }

        if (BizEmpty(tUserOpenId.getAccessId())){
            throw  new BizException(ApiResultCode.COMMON_PARAM_NULL);
        }
       TUserOpenId tUserOpenId1 =     tUserMappers.getOpenId(tUserOpenId);

        log.info(JSON.toJSONString(tUserOpenId1));
        if (BizEmpty(tUserOpenId1)){
            tUserMappers.insertOpenId(tUserOpenId);
        }else {
            log.info("根据手机号或者uid去修改信息");
            //根据手机号或者uid去修改信息
            tUserMappers.UpdateOpenId(tUserOpenId);
            return "ok";
        }


      return  "ok";
    }

    /**
     * 判断空值
     */
    public boolean isNULL(TUser tUser) {
        boolean str = true;
        if (BizEmpty(tUser.getUid())) {
            return false;
        }
        if (BizEmpty(tUser.getImage())) {
            return false;
        }
        if (BizEmpty(tUser.getNickName())) {
            return false;
        }
        if (BizEmpty(tUser.getAccount())) {
            return false;
        }
        if (BizEmpty(tUser.getPhone())) {
            return false;
        }
        if (BizEmpty(tUser.getSex())) {
            return false;
        }
        if (BizEmpty(tUser.getStatus())) {
            return false;
        }


        return str;
    }
}
